﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using WWLauncher.Libs.WindowsInput.Native;

namespace WWLauncher.Util
{
    public class ScreenUtil
    {
         
        //取得桌面窗口句柄函数 
        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();
        //取得Shell窗口句柄函数 
        [DllImport("user32.dll")]
        private static extern IntPtr GetShellWindow();



        //获取类的名字
        [DllImport("user32.dll")]
        private static extern int GetClassName(
            IntPtr hWnd,//句柄
            StringBuilder lpString, //类名
            int nMaxCount); //最大值

        /// <summary>
        /// 判断当前屏幕(鼠标最后活动屏幕)是否有全屏化应用
        /// </summary>
        /// <returns></returns>
        public static bool IsPrimaryFullScreen()
        {
            var desktopHandle = GetDesktopWindow();
            var shellHandle = GetShellWindow();
            bool runningFullScreen = false;
            var hWnd = WinApi.GetForegroundWindow();

            StringBuilder sb = new StringBuilder(256);
            try
            {
                GetClassName(hWnd, sb, sb.Capacity);
            }
            catch
            {
                // ignored
            }

            if (sb.ToString().ToLower().Equals("workerw"))
                return false;
            if (!hWnd.Equals(IntPtr.Zero))
            {
                //判断是否桌面或shell        
                if (!(hWnd.Equals(desktopHandle) || hWnd.Equals(shellHandle)))
                {
                    //取得窗口大小 
                    WinApi.GetWindowRect(hWnd, out var appBounds);
                    //判断是否全屏 
                    var screenBounds = Screen.FromHandle(hWnd).Bounds;
                    if (appBounds.Bottom - appBounds.Top == screenBounds.Height
                        && (appBounds.Right - appBounds.Left) == screenBounds.Width)
                    {
                        runningFullScreen = true;
                    }
                }
            }
            return runningFullScreen;
        }

        [DllImport("gdi32")]
        static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

        public const int DESKTOPVERTRES = 117;
        /// <summary>
        /// 获取屏幕缩放比例
        /// </summary>
        /// <returns></returns>
        public static double GetScreenScalingFactor()
        {
            try
            {
                var g = Graphics.FromHwnd(IntPtr.Zero);
                IntPtr desktop = g.GetHdc();
                var physicalScreenHeight = GetDeviceCaps(desktop, DESKTOPVERTRES);

                var screenScalingFactor =
                    physicalScreenHeight / SystemParameters.PrimaryScreenHeight;
                //SystemParameters.PrimaryScreenHeight;

                return screenScalingFactor;
            }
            catch
            {
                return 1;
            }

        }

    }
}
